<?php
/**
 * Punny - The most easy-to-use PHP MVC framework
 * 
 * Copyright (c) 2010  
 *
 * @author email :lx.xin@qq.com
 */
 class Admin_LoginController extends Zend_Controller_Action{
	function init(){
		$this->registry = Zend_Registry::getInstance();
        $this->view = $this->registry['view'];
		$this->view->setScriptPath('./application/views/scripts/admin/');
		//$tpl=$this->_request->getBaseUrl();
        $this->view->baseUrl = $this->_request->getBaseUrl();	
	}

	function indexAction(){
		if(strtolower($_SERVER['REQUEST_METHOD'])=='post')
			{ 
			Zend_Loader::loadClass('Zend_Filter_StripTags');
			$filter=new Zend_Filter_StripTags();
			//表单的post值
			$username = $filter->filter($this->_request->getPost('username'));
			$password = $filter->filter($this->_request->getPost('password'));
			//$validcode = $filter->filter($this->_request->getPost('validcode'));//验证码
			//echo $validcode; exit;			
			$user=new User();
			$db = $user->getAdapter();;
			$authAdapter = new Zend_Auth_Adapter_DbTable($db);
			$authAdapter->setTableName('my_user')//数据库表名
			// 数据库表的列的名称，用来表示身份。身份列必须包含唯一的值，例如用户名或者e-mail地址。 
						->setIdentityColumn('username')
			//数据库表的列的名称，用来表示证书。在一个简单的身份和密码认证scheme下，证书的值对应为密码
						->setCredentialColumn('password')
						->setIdentity($username)//认证的值
						->setCredential($password);

			$auth = Zend_Auth::getInstance();
			$result = $auth->authenticate($authAdapter);// 执行认证查询，并保存结果
			// 输出身份
			//echo $result->getIdentity() . "\n\n";
			// 输出结果行
			//echo '<pre>';print_r($authAdapter->getResultRowObject());exit;

			if($result->isValid())//isValid() - 返回 true 当且仅当结果表示一个成功的认证尝试 
			{
				$data = $authAdapter->getResultRowObject(null,'password');
				//echo '<pre>';print_r($data);exit;
				if($auth->hasIdentity() )//成功登录
				{
					//auth之后写入session
					$_SESSION['username']=$username;					
					//$user->setExpirationSeconds(3600);//命名空间 "user" 将在第一次访问后 60 秒过期
					$url=$this->_request->getBaseUrl()."/admin/index/";
					$this->Get_admin_msg($url,"登陆成功");
				}
			}else{
				$this->view->msg='<h3><font color=red>Login Error! Please try again!</font></h3>';
				$this->view->url=$this->_request->getBaseUrl()."/admin/index/login";
				echo $this->view->render('error.html');					  
				}			
		}else{
			echo $this->view->render('login.html');
		}
	}

	function loginoutAction(){		
		session_destroy();;//销毁session
		$url=$this->_request->getBaseUrl()."/admin/login";
		$this->Get_admin_msg($url,"退出成功");
	}

	function Get_admin_msg($url, $show = '操作已成功！') {
		$msg = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
		<html xmlns="http://www.w3.org/1999/xhtml"><head>
				<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
				<link rel="stylesheet" href="css/common.css" type="text/css" />
				<meta http-equiv="refresh" content="5; URL=' . $url . '" />
				<title>管理区域</title>
				</head>
				<body>
				<div id="man_zone">
				  <table width="30%" border="1" align="center"  cellpadding="3" cellspacing="0" class="table" style="margin-top:100px;">
				    <tr>
				      <th align="center" style="background:#cef">信息提示</th>
				    </tr>
				    <tr>
				      <td><p>' . $show . '<br />
				      5秒后返回指定页面！<br />
				      如果浏览器无法跳转，<a href="' . $url . '">请点击此处</a>。</p></td>
				    </tr>
				  </table>
				</div>
				</body>
				</html>';
		echo $msg;
		exit ();
	}
 }






 ?>